package com.jwds.digital.bios.business.core.utils;

import com.jwds.tdc.framework.application.lib.BaseResponse;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import com.jwds.tdc.framework.common.exception.BusinessException;

import java.util.List;

/**
 * feign返回处理公共方法
 *
 * @Author hesong
 */
public class FeignResultUtil {

    /**
     * 返回结果通用处理
     *
     * @param baseResponse 返回封装对象
     * @param <T>          数据类型
     * @return 数据对象
     */
    public static <T> T getDataFromBaseResponse(BaseResponse<T> baseResponse) {
        if (baseResponse.isSuccess()) {
            return baseResponse.getData();
        } else {
            throw new BusinessException(baseResponse.getCode(), baseResponse.getMessage());
        }
    }


    /**
     * 替换PageResponse中的数据集合
     *
     * @param dataList
     * @param oldPageResponse
     * @return
     */
    public static PageResponse turnDataList(List dataList, PageResponse oldPageResponse) {
        PageResponse newPageResponse = new PageResponse<>();
        newPageResponse.setTotalPage(oldPageResponse.getTotalPage());
        newPageResponse.setTotalCount(oldPageResponse.getTotalCount());
        newPageResponse.setPageSize(oldPageResponse.getPageSize());
        newPageResponse.setBeginPage(oldPageResponse.getBeginPage());
        newPageResponse.setLists(dataList);
        return newPageResponse;
    }
}
